package com.itheima.utils;

import com.itheima.config.MyJwtPropeeties;
import io.jsonwebtoken.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.security.Signature;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class MyJwtUtil {
    @Autowired
    private MyJwtPropeeties propeeties;
    public String creatJwt(Map<String,Object> map){
       String mygirl = Jwts.builder()
                .setClaims(map)
                .signWith(SignatureAlgorithm.valueOf(propeeties.getSignType()), propeeties.getKey())  //转成枚举类型
                .setExpiration(new Date(System.currentTimeMillis() + propeeties.getTime()))
                .compact();
       return mygirl;
    }

    public Map<String,Object> parseJwt(String jwt){
        Claims mygirl1 = null;
        try {
            mygirl1 = Jwts.parser()
                    .setSigningKey(propeeties.getKey())
                    .parseClaimsJws(jwt)
                    .getBody();
        } catch (Exception e) {
            //仅仅只是打印异常信息
            e.printStackTrace();
        }
        return mygirl1;
    }
}
